package com.like.scan;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;

import com.google.zxing.ResultPoint;
import com.journeyapps.barcodescanner.ViewfinderView;

import java.util.ArrayList;
import java.util.List;

public class CustomViewfinderView extends ViewfinderView {

    private int mBorderWidth = 17;
    private int mBorderHeight = 5;

    public CustomViewfinderView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setBorderWidthDP(int width) {
        this.mBorderWidth = width;
    }

    public void setBorderHeightDP(int height) {

    }

    @SuppressLint("DrawAllocation")
    @Override
    public void onDraw(Canvas canvas) {
        refreshSizes();
        if (framingRect == null || previewFramingRect == null) {
            return;
        }
        paint.setColor(0xffecfe);
        Rect frame = framingRect;
        Rect previewFrame = previewFramingRect;

        int width = canvas.getWidth();
        int height = canvas.getHeight();

        // Draw the exterior (i.e. outside the framing rect) darkened
        paint.setColor(resultBitmap != null ? resultColor : maskColor);
        canvas.drawRect(0, 0, width, frame.top, paint);
        canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint);
        canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, paint);
        canvas.drawRect(0, frame.bottom + 1, width, height, paint);

        paint.setColor(laserColor);
        int lineHeight = dp2px(getContext(), mBorderHeight);
        int lineWidth = dp2px(getContext(), mBorderWidth);
        // 画左上角的线
        canvas.drawRect(frame.left - lineHeight, frame.top - lineHeight,
                frame.left + lineWidth, frame.top, paint);
        canvas.drawRect(frame.left - lineHeight, frame.top - lineHeight,
                frame.left, frame.top + lineWidth, paint);
        // 画右上角的线
        canvas.drawRect(frame.right - lineWidth, frame.top - lineHeight,
                frame.right + lineHeight, frame.top, paint);
        canvas.drawRect(frame.right, frame.top - lineHeight, frame.right
                + lineHeight, frame.top + lineWidth, paint);
        // 图左下角
        canvas.drawRect(frame.left - lineHeight, frame.bottom - lineWidth,
                frame.left, frame.bottom + lineHeight, paint);
        canvas.drawRect(frame.left - lineHeight, frame.bottom, frame.left
                + lineWidth, frame.bottom + lineHeight, paint);
        // 图右下角
        canvas.drawRect(frame.right, frame.bottom - lineWidth, frame.right
                + lineHeight, frame.bottom + lineHeight, paint);
        canvas.drawRect(frame.right - lineWidth, frame.bottom, frame.right
                + lineHeight, frame.bottom + lineHeight, paint);

        if (resultBitmap != null) {
            // Draw the opaque result bitmap over the scanning rectangle
            paint.setAlpha(CURRENT_POINT_OPACITY);
            canvas.drawBitmap(resultBitmap, null, frame, paint);
        } else {
            // Draw a red "laser scanner" line through the middle to show decoding is active
            paint.setColor(laserColor);
            paint.setAlpha(SCANNER_ALPHA[scannerAlpha]);
            scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length;
            int middle = frame.height() / 2 + frame.top;
//            canvas.drawRect(frame.left + 2, middle - 1, frame.right - 1, middle + 2, paint);

            int laserPadding = 30;
            int laseHeight = 10;
            canvas.drawRect(frame.left + laserPadding, middle - laseHeight
                    / 2, frame.right - laserPadding, middle + laseHeight
                    / 2, paint);

            float scaleX = frame.width() / (float) previewFrame.width();
            float scaleY = frame.height() / (float) previewFrame.height();

            List<ResultPoint> currentPossible = possibleResultPoints;
            List<ResultPoint> currentLast = lastPossibleResultPoints;
            int frameLeft = frame.left;
            int frameTop = frame.top;
            if (currentPossible.isEmpty()) {
                lastPossibleResultPoints = null;
            } else {
                possibleResultPoints = new ArrayList<>(5);
                lastPossibleResultPoints = currentPossible;
                paint.setAlpha(CURRENT_POINT_OPACITY);
                paint.setColor(resultPointColor);
                for (ResultPoint point : currentPossible) {
                    canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX),
                            frameTop + (int) (point.getY() * scaleY),
                            POINT_SIZE, paint);
                }
            }
            if (currentLast != null) {
                paint.setAlpha(CURRENT_POINT_OPACITY / 2);
                paint.setColor(resultPointColor);
                float radius = POINT_SIZE / 2.0f;
                for (ResultPoint point : currentLast) {
                    canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX),
                            frameTop + (int) (point.getY() * scaleY),
                            radius, paint);
                }
            }

            // Request another update at the animation interval, but only repaint the laser line,
            // not the entire viewfinder mask.
            postInvalidateDelayed(ANIMATION_DELAY,
                    frame.left - POINT_SIZE,
                    frame.top - POINT_SIZE,
                    frame.right + POINT_SIZE,
                    frame.bottom + POINT_SIZE);
        }
    }

    private int dp2px(Context context, float dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpVal, context.getResources().getDisplayMetrics());
    }
}
